package com.wephone.Student.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.data.CityData;
import com.wephone.Student.bean.data.CountryData;
import com.wephone.Student.bean.data.ProvinceData;
import com.wephone.Student.bean.savedData.HouseSaveData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.Util;
import com.wephone.Student.wheel.widget.OnWheelChangedListener;
import com.wephone.Student.wheel.widget.StringWheelAdapter;
import com.wephone.Student.wheel.widget.WheelView;

public class CreateHouseInfoActivity extends BaseActivity {
	private EditText inputEdit1;
	private TextView inputEdit2;
	private EditText inputEdit3;
	private EditText inputEdit4;
	private EditText inputEdit5;
	private EditText inputEdit6;
	private EditText inputEdit7;
	private RadioButton houseTypeRadio1;
	private RadioButton houseTypeRadio2;
	private RadioButton houseTypeRadio3;
	private RadioButton houseSourceRadio1;
	private RadioButton houseSourceRadio2;
//	private ImageView houseInfoImage;
//	private File photoTempFile = new File(Constants.IMAGE_PHOTO_TMP_PATH+"tmpPic.jpg");
//	private File photoSavedFile = null;
//	private String photoSavedName = null;
//	private boolean hasPhotoUpload = false;
	private ProgressDialog progressDialog;
	private ImageView subPic1;
	private ImageView subPic2;
	private ImageView subPic3;
	private boolean hasSubPic1Changed = false;
	private boolean hasSubPic2Changed = false;
	private boolean hasSubPic3Changed = false;
	private File subSavedFile1 = null;
	private String subSavedName1 = null;
	private File subSavedFile2 = null;
	private String subSavedName2 = null;
	private File subSavedFile3 = null;
	private String subSavedName3 = null;
	private static HouseSaveData houseSaveData = new HouseSaveData();
	private boolean notSave = false;
	private int nCountryIdx;
	private int nProvinceIdx;
	private int nCityIdx;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(CreateHouseInfoActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				if(getDataSuccess){
//					FlyTogetherListResponse res = (FlyTogetherListResponse)response;
					notSave = true;
					setResult(222);
					finish();
					if(subSavedFile1!=null && subSavedFile1.exists()){
						subSavedFile1.delete();
					}
					if(subSavedFile2!=null && subSavedFile2.exists()){
						subSavedFile2.delete();
					}
					if(subSavedFile3!=null && subSavedFile3.exists()){
						subSavedFile3.delete();
					}
				}
			}
			
			@Override
			public void onFailure(Throwable arg0, String result) {
				super.onFailure(arg0, result);
				
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				Toast.makeText(getApplicationContext(), getString(R.string.network_error_txt), Toast.LENGTH_SHORT).show();
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_house_info);
		
		init();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		inputEdit1.setText(houseSaveData.getInputStr1());
		inputEdit2.setText(houseSaveData.getInputStr2());
		inputEdit3.setText(houseSaveData.getInputStr3());
		inputEdit4.setText(houseSaveData.getInputStr4());
		inputEdit5.setText(houseSaveData.getInputStr5());
		inputEdit6.setText(houseSaveData.getInputStr6());
		inputEdit7.setText(houseSaveData.getInputStr7());
		if(houseSaveData.getRadioIndx1()==0){
			houseTypeRadio1.setChecked(true);
		}else if(houseSaveData.getRadioIndx1()==1){
			houseTypeRadio2.setChecked(true);
		}else{
			houseTypeRadio3.setChecked(true);
		}
		if(houseSaveData.getRadioIndx2()==0){
			houseSourceRadio1.setChecked(true);
		}else{
			houseSourceRadio2.setChecked(true);
		}
		nCountryIdx = houseSaveData.getnCountryIdx();
		nProvinceIdx = houseSaveData.getnProvinceIdx();
		nCityIdx = houseSaveData.getnCityIdx();
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		if(!notSave){
			houseSaveData.setInputStr1(inputEdit1.getText().toString());
			houseSaveData.setInputStr2(inputEdit2.getText().toString());
			houseSaveData.setInputStr3(inputEdit3.getText().toString());
			houseSaveData.setInputStr4(inputEdit4.getText().toString());
			houseSaveData.setInputStr5(inputEdit5.getText().toString());
			houseSaveData.setInputStr6(inputEdit6.getText().toString());
			houseSaveData.setInputStr7(inputEdit7.getText().toString());
			if(houseTypeRadio1.isChecked()){
				houseSaveData.setRadioIndx1(0);
			}else if(houseTypeRadio2.isChecked()){
				houseSaveData.setRadioIndx1(1);
			}else{
				houseSaveData.setRadioIndx1(2);
			}
			if(houseSourceRadio1.isChecked()){
				houseSaveData.setRadioIndx2(0);
			}else{
				houseSaveData.setRadioIndx2(1);
			}
			houseSaveData.setnCountryIdx(nCountryIdx);
			houseSaveData.setnProvinceIdx(nProvinceIdx);
			houseSaveData.setnCityIdx(nCityIdx);
		}else{
			houseSaveData = new HouseSaveData();
		}
	}

	private void init(){
		inputEdit1 = (EditText)findViewById(R.id.create_houese_input_edit_1);
		inputEdit2 = (TextView)findViewById(R.id.create_houese_input_edit_2);
		inputEdit3 = (EditText)findViewById(R.id.create_houese_input_edit_3);
		inputEdit4 = (EditText)findViewById(R.id.create_houese_input_edit_4);
		inputEdit5 = (EditText)findViewById(R.id.create_houese_input_edit_5);
		inputEdit6 = (EditText)findViewById(R.id.create_houese_input_edit_6);
		inputEdit7 = (EditText)findViewById(R.id.create_houese_input_edit_7);
		houseTypeRadio1 = (RadioButton)findViewById(R.id.house_type_radio_1);
		houseTypeRadio2 = (RadioButton)findViewById(R.id.house_type_radio_2);
		houseTypeRadio3 = (RadioButton)findViewById(R.id.house_type_radio_3);
		houseSourceRadio1 = (RadioButton)findViewById(R.id.house_source_radio_1);
		houseSourceRadio2 = (RadioButton)findViewById(R.id.house_source_radio_2);
		subPic1 = (ImageView)findViewById(R.id.house_info_pic_1);
		subPic2 = (ImageView)findViewById(R.id.house_info_pic_2);
		subPic3 = (ImageView)findViewById(R.id.house_info_pic_3);
		
		inputEdit2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showChooseCountryDialog5(getString(R.string.create_house_info_item_4));
			}
		});
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(checkInput()){
					progressDialog = ProgressDialog.show(CreateHouseInfoActivity.this, "提示", "正在提交数据...", true, false);
					
					RequestParams params = new RequestParams();
					params.put("hUid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					if(houseTypeRadio1.isChecked()){
						params.put("hSaleType", "0");
					}else if(houseTypeRadio2.isChecked()){
						params.put("hSaleType", "1");
					}else{
						params.put("hSaleType", "2");
					}
					if(houseSourceRadio1.isChecked()){
						params.put("hSaleSource", "0");
					}else{
						params.put("hSaleSource", "1");
					}
					params.put("hTs", ""+System.currentTimeMillis()/1000);
					params.put("hTitle", inputEdit1.getText().toString());
					params.put("hLoc", inputEdit2.getText().toString());
					params.put("hHouseType", inputEdit3.getText().toString());
					params.put("hAddr", inputEdit4.getText().toString());
					params.put("hPrice", inputEdit5.getText().toString());
					params.put("hDetail", inputEdit6.getText().toString());
					params.put("hTel", inputEdit7.getText().toString());
					if(hasSubPic1Changed && subSavedFile1!=null && subSavedFile1.exists()){
						try {
							params.put("hImg", subSavedFile1);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						}
					}
					if(hasSubPic2Changed && subSavedFile2!=null && subSavedFile2.exists()){
						try {
							params.put("hImg1", subSavedFile2);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						}
					}
					if(hasSubPic3Changed && subSavedFile3!=null && subSavedFile3.exists()){
						try {
							params.put("hImg2", subSavedFile3);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						}
					}
					HttpClient.getInstance().post(Constants.API_ADD_HOUSE_SALES_URL, params, getHandler());
				}
			}
		});
		
		subPic1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(CreateHouseInfoActivity.this).setTitle("添加图片")
				.setItems(new String[]{"拍照","相册","还原"}, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						if(arg1==0){
							File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
							if(!rootFile.exists()){
								rootFile.mkdirs();
							}
							subSavedName1 = "trans_"+System.currentTimeMillis()+".jpg";
							subSavedFile1 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName1);
							Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
							intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(subSavedFile1));
		                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		                    startActivityForResult(intent, 101);
						}else if(arg1==1){
							subSavedName1 = "trans_"+System.currentTimeMillis()+".jpg";
							subSavedFile1 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName1);
							Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
							startActivityForResult(i, 102);
						}else{
							if(subSavedFile1.exists()){
								subSavedFile1.delete();
							}
							subSavedFile1=null;
							subPic1.setImageResource(R.drawable.default_add_picture);
						}
					}})
				.setNegativeButton("取消", null).show();
			}
		});
		subPic2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(CreateHouseInfoActivity.this).setTitle("添加图片")
				.setItems(new String[]{"拍照","相册","还原"}, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						if(arg1==0){
							File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
							if(!rootFile.exists()){
								rootFile.mkdirs();
							}
							subSavedName2 = "trans_"+System.currentTimeMillis()+".jpg";
							subSavedFile2 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName2);
							Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
							intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(subSavedFile2));
		                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		                    startActivityForResult(intent, 201);
						}else if(arg1==1){
							subSavedName2 = "trans_"+System.currentTimeMillis()+".jpg";
							subSavedFile2 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName2);
							Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
							startActivityForResult(i, 202);
						}else{
							if(subSavedFile2.exists()){
								subSavedFile2.delete();
							}
							subSavedFile2=null;
							subPic2.setImageResource(R.drawable.default_add_picture);
						}
					}})
				.setNegativeButton("取消", null).show();
			}
		});
		subPic3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(CreateHouseInfoActivity.this).setTitle("添加图片")
				.setItems(new String[]{"拍照","相册","还原"}, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						if(arg1==0){
							File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
							if(!rootFile.exists()){
								rootFile.mkdirs();
							}
							subSavedName3 = "trans_"+System.currentTimeMillis()+".jpg";
							subSavedFile3 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName3);
							Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
							intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(subSavedFile3));
		                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		                    startActivityForResult(intent, 301);
						}else if(arg1==1){
							subSavedName3 = "trans_"+System.currentTimeMillis()+".jpg";
							subSavedFile3 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName3);
							Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
							startActivityForResult(i, 302);
						}else{
							if(subSavedFile3.exists()){
								subSavedFile3.delete();
							}
							subSavedFile3=null;
							subPic3.setImageResource(R.drawable.default_add_picture);
						}
					}})
				.setNegativeButton("取消", null).show();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode==101){
			if(subSavedFile1!=null && subSavedFile1.exists()){
				hasSubPic1Changed = true;
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(subSavedFile1.getPath()), 300, 300);
				Bitmap bitmap = Util.decodeFile(subSavedFile1.getPath());
				subPic1.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,subSavedName1,getApplicationContext());
			}
   	 	}else if(requestCode==102){
	   	 	if(data!=null){
		   	 	Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 hasSubPic1Changed = true;
//		        		 Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picturePath), 300, 300);
		        		 Bitmap bitmap = Util.decodeFile(picturePath);
		        		 subPic1.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,subSavedName1,getApplicationContext());
		        	 }
		         }
	   	 	}
   	 	}else if(requestCode==201){
			if(subSavedFile2!=null && subSavedFile2.exists()){
				hasSubPic2Changed = true;
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(subSavedFile2.getPath()), 300, 300);
				Bitmap bitmap = Util.decodeFile(subSavedFile2.getPath());
				subPic2.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,subSavedName2,getApplicationContext());
			}
   	 	}else if(requestCode==202){
	   	 	if(data!=null){
		   	 	Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 hasSubPic2Changed = true;
//		        		 Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picturePath), 300, 300);
		        		 Bitmap bitmap = Util.decodeFile(picturePath);
		        		 subPic2.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,subSavedName2,getApplicationContext());
		        	 }
		         }
	   	 	}
   	 	}else if(requestCode==301){
			if(subSavedFile3!=null && subSavedFile3.exists()){
				hasSubPic3Changed = true;
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(subSavedFile3.getPath()), 300, 300);
				Bitmap bitmap = Util.decodeFile(subSavedFile3.getPath());
				subPic3.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,subSavedName3,getApplicationContext());
			}
   	 	}else if(requestCode==302){
	   	 	if(data!=null){
		   	 	Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 hasSubPic3Changed = true;
//		        		 Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picturePath), 300, 300);
		        		 Bitmap bitmap = Util.decodeFile(picturePath);
		        		 subPic3.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,subSavedName3,getApplicationContext());
		        	 }
		         }
	   	 	}
   	 	}
	}
	
	private boolean checkInput(){
		if(TextUtils.isEmpty(inputEdit1.getText().toString().trim().replace("\n", ""))){
			Toast.makeText(getApplicationContext(), "标题必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit2.getText().toString())){
			Toast.makeText(getApplicationContext(), "城市必须填写", Toast.LENGTH_SHORT).show();
			return false;
//		}else if(TextUtils.isEmpty(inputEdit3.getText().toString())){
//			Toast.makeText(getApplicationContext(), "户型必须选择", Toast.LENGTH_SHORT).show();
//			return false;
//		}else if(TextUtils.isEmpty(inputEdit4.getText().toString())){
//			Toast.makeText(getApplicationContext(), "详细地址必须填写", Toast.LENGTH_SHORT).show();
//			return false;
		}else if(!houseTypeRadio2.isChecked() && TextUtils.isEmpty(inputEdit5.getText().toString())){
			Toast.makeText(getApplicationContext(), "价格必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit6.getText().toString().trim().replace("\n", ""))){
			Toast.makeText(getApplicationContext(), "详情必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}
//		else if(TextUtils.isEmpty(inputEdit7.getText().toString())){
//			Toast.makeText(getApplicationContext(), "电话必须填写", Toast.LENGTH_SHORT).show();
//			return false;
//		}
		return true;
	}
	
	private void showChooseCountryDialog5(String title){
		View inputView = getLayoutInflater().inflate(R.layout.dialog_wheel_3_view, null);
		final WheelView wheel1 = (WheelView)inputView.findViewById(R.id.wheel_1);
		final WheelView wheel2 = (WheelView)inputView.findViewById(R.id.wheel_2);
		final WheelView wheel3 = (WheelView)inputView.findViewById(R.id.wheel_3);
		
		List<String> countryList = new ArrayList<String>();
		for(CountryData cd:BaseActivity.getCountryList(getApplicationContext())){
			countryList.add(cd.getN());
		}
        wheel1.setAdapter(new StringWheelAdapter(countryList));
        wheel1.setCurrentItem(nCountryIdx);
        wheel1.setCyclic(true);
        wheel1.setInterpolator(new AnticipateOvershootInterpolator());
        wheel1.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nCountryIdx = newValue;
				nProvinceIdx = 0;
				nCityIdx = 0;
				
				List<String> provinceList = new ArrayList<String>();
				for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP()){
					provinceList.add(cd.getN());
				}
		        wheel2.setAdapter(new StringWheelAdapter(provinceList));
		        wheel2.setCurrentItem(nProvinceIdx);
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx);
			}});
		
		List<String> provinceList = new ArrayList<String>();
		for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP()){
			provinceList.add(cd.getN());
		}
        wheel2.setAdapter(new StringWheelAdapter(provinceList));
        wheel2.setCurrentItem(nProvinceIdx);
        wheel2.setCyclic(true);
        wheel2.setInterpolator(new AnticipateOvershootInterpolator());
        wheel2.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nProvinceIdx = newValue;
				nCityIdx = 0;
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx);
			}});
        
		List<String> cityList = new ArrayList<String>();
		for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
			cityList.add(cd.getN());
		}
        wheel3.setAdapter(new StringWheelAdapter(cityList));
        wheel3.setCurrentItem(nCityIdx);
        wheel3.setCyclic(true);
        wheel3.setInterpolator(new AnticipateOvershootInterpolator());
        
		AlertDialog.Builder builder = new AlertDialog.Builder(CreateHouseInfoActivity.this);
		builder.setTitle(title);
		builder.setView(inputView);  
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				nCountryIdx = wheel1.getCurrentItem();
				nProvinceIdx = wheel2.getCurrentItem();
				nCityIdx = wheel3.getCurrentItem();
				if(TextUtils.isEmpty(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN())){
					inputEdit2.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
				}else{
					inputEdit2.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
				}
			}
		});  
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){}
		}); 
		builder.show();  
	}

}
